package com.colli;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ColliCalcActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
               
        final Button bt0 = (Button) findViewById(R.id.button0);
        final Button bt1 = (Button) findViewById(R.id.button1);
        final Button bt2 = (Button) findViewById(R.id.button2);
        final Button bt3 = (Button) findViewById(R.id.button3);
        final Button bt4 = (Button) findViewById(R.id.button4);
        final Button bt5 = (Button) findViewById(R.id.button5);
        final Button bt6 = (Button) findViewById(R.id.button6);
        final Button bt7 = (Button) findViewById(R.id.button7);
        final Button bt8 = (Button) findViewById(R.id.button8);
        final Button bt9 = (Button) findViewById(R.id.button9);
        final Button btAdd = (Button) findViewById(R.id.buttonAdd);
        final Button btSub = (Button) findViewById(R.id.buttonSub);
        final Button btMul = (Button) findViewById(R.id.buttonMul);
        final Button btDiv = (Button) findViewById(R.id.buttonDiv);
        final Button btPer = (Button) findViewById(R.id.buttonPer);
        final Button btSqr = (Button) findViewById(R.id.buttonSqr);
        final Button btExp = (Button) findViewById(R.id.buttonExp);
        final Button btEqu = (Button) findViewById(R.id.buttonEqu);
        final Button btDot = (Button) findViewById(R.id.buttonDot);
        final Button btPar1 = (Button) findViewById(R.id.buttonPar1);
        final Button btPar2 = (Button) findViewById(R.id.buttonPar2);
        final Button btClear = (Button) findViewById(R.id.buttonClear);
        final EditText txtResult = (EditText) findViewById(R.id.txtResult);
        
        txtResult.setOnKeyListener(new OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					if ((keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9)
						|| keyCode == KeyEvent.KEYCODE_PLUS
						|| keyCode == KeyEvent.KEYCODE_MINUS
						|| keyCode == KeyEvent.KEYCODE_PERIOD)
						return true;
					else {
						switch (keyCode) {
						case KeyEvent.KEYCODE_STAR:
							txtResult.setText(txtResult.getText() + "x");							
							break;
						case KeyEvent.KEYCODE_SLASH:
							txtResult.setText(txtResult.getText() + "÷");							
							break;
						
						default:
							break;
							
						}
						return false;
					}
				}
				else
					return false;
				
			}
		});
        
        bt0.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
				// TODO Auto-generated method stub
        		        		
        		txtResult.setText(txtResult.getText() + "0");
        	}
        });
        
        bt1.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
				// TODO Auto-generated method stub
        		        		
        		txtResult.setText(txtResult.getText() + "1");
        	}
        });
        
        
        bt2.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
				// TODO Auto-generated method stub
        		        		
        		txtResult.setText(txtResult.getText() + "2");
        	}
        });
        
        bt3.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
				// TODO Auto-generated method stub
        		        		
        		txtResult.setText(txtResult.getText() + "3");
        	}
        });
        
        bt4.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
				// TODO Auto-generated method stub
        		        		
        		txtResult.setText(txtResult.getText() + "4");
        	}
        });
        
        bt5.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
				// TODO Auto-generated method stub
        		        		
        		txtResult.setText(txtResult.getText() + "5");
        	}
        });
        
        bt6.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
				// TODO Auto-generated method stub
        		        		
        		txtResult.setText(txtResult.getText() + "6");
        	}
        });
        
        bt7.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
				// TODO Auto-generated method stub
        		        		
        		txtResult.setText(txtResult.getText() + "7");
        	}
        });
        
        bt8.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
				// TODO Auto-generated method stub
        		        		
        		txtResult.setText(txtResult.getText() + "8");
        	}
        });
        
        bt9.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
				// TODO Auto-generated method stub
        		        		
        		txtResult.setText(txtResult.getText() + "9");
        	}
        });
        
        btAdd.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
				// TODO Auto-generated method stub
        		        		
        		txtResult.setText(txtResult.getText() + "+");
        	}
        });
        
        btSub.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
				// TODO Auto-generated method stub
        		        		
        		txtResult.setText(txtResult.getText() + "-");
        	}
        });
        
        btMul.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
				// TODO Auto-generated method stub
        		        		
        		txtResult.setText(txtResult.getText() + "x");
        	}
        });
        
        btDiv.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
				// TODO Auto-generated method stub
        		        		
        		txtResult.setText(txtResult.getText() + "÷");
        	}
        });
        
        btPer.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
				// TODO Auto-generated method stub
        		        		
        		txtResult.setText(txtResult.getText() + "%");
        	}
        });
        
        btExp.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
				// TODO Auto-generated method stub
        		        		
        		txtResult.setText(txtResult.getText() + "^");
        	}
        });
        
        btSqr.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
				// TODO Auto-generated method stub
        		        		
        		txtResult.setText(txtResult.getText() + "√");
        	}
        });
        
        btDot.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
				// TODO Auto-generated method stub
        		        		
        		txtResult.setText(txtResult.getText() + ".");
        	}
        });
        
        btPar1.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
				// TODO Auto-generated method stub
        		        		
        		txtResult.setText(txtResult.getText() + "(");
        	}
        });
        
        btPar2.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
				// TODO Auto-generated method stub
        		        		
        		txtResult.setText(txtResult.getText() + ")");
        	}
        });
        
        btClear.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
				// TODO Auto-generated method stub
        		        		
        		txtResult.setText("");
        	}
        });
        
        btEqu.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
				// TODO Auto-generated method stub
        		        		
        		txtResult.setText("Resultado: " + txtResult.getText());
        	}
        });
        
        
    }
}